<?php
/**
 * Custom DjTpl tags and filters
 * tag {% urlchange path var1=path var2="value" %}
 *
 * @link http://webappkit.net
 * @package webappkit
 * @subpackage db
 * @author J.Ducastel <nospam0@ducastel.name>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */
class DjTplTagNode_UrlChange extends DjTplTagNode {

    /**
    * builds itself from tokens
    * @param djtplToken &$selfToken
    * @param array &$nextTokens djangoTplToken
    * @access public
    * @return bool
    */
    function buildFromTokens(&$selfToken) {
        // $this->token=&$selfToken;
        $def = trim($selfToken->tagdef);
        if ($def)
            $def = explode(' ',$def); // print_r($def);
        else
            return true;
        // searching path in first expression
        if (ereg('^[^=]+$',$def[0])) {
            // no '=', is a path
            $this->path = array_shift($def);
        }
        // everything else should be GET vars expressions : var=source or var="value"
        foreach($def as $expr) {
            list($key, $source) = explode('=',$expr);
            $this->get_vars[$key] = $source;
        }

        return true;
    }/**/

    /**
    * is the node atomic (has no children)
    * @access public
    * @return bool
    */
    function isAtomic() {
        return true;
    }/**/

    /**
    * renders as string
    * @param djangoTplContext $context
    * @access public
    * @return string
    */
    function render(&$context) {
        $req = HttpRequestBean::getCurrent();
        $url = $req->get('url');
        if ($this->path);
            $url->setpath($this->path);
        foreach ($this->get_vars as $key => $source) {
            $value = djTplParser::evalExpression($context, $source);
            $url->setvar($key, $value);
        }
        return $url->getAbsolute();
    }/**/

    var $tagname = 'urlchange';

    /**
     * @var string collection path
     * @access protected
     */
    var $path;

    /**
     * @var array get vars replacement key => source
     */
    var $get_vars=array();
}

/**
 * changes a get variable's value within source url
 * /
function djtplFilter_urlVar($value, $arg) {
    if (!is_a($value,'UrlBean'))
        return false;
    $arg
}/**/